GBML manual / Events

When each event runs

Every event GBML has, in the order the game runs them during one frame, with a link to the page for each.

What this page is for

Events explains what an event is: a piece of code the game calls at the moment its name promises, so you write the code and never call it yourself. This page is the calendar. It lists every event there is, says when in a frame each one runs, and links to the page for each. The order of a frame draws the same sequence as a diagram; this page adds where every event sits in it, including the ones that run outside the frame loop, and ends with the order everything happens in when a room opens.

A frame is one pass of the game's main loop. The GBA draws 60 of them a second, and in each one the game runs your events, moves things, and draws the screen. Which event runs first matters whenever two of them touch the same variable.

One frame, in order

StepWhat happensEvents that run
1the frame startsStep Begin
2alarm countdownsAlarm 0 to 11, for each alarm that reaches 0
3the buttons are readButton Pressed, Held and Released
4the main eventStep
5things move, then follow pathsPath End, when a path finishes
6position is checked against the room and the viewBeyond Room, Hit Room Edge, Beyond View, Inside View
7overlaps are found, then reportedCollision with an object
8the frame's logic endsStep End
9the frame is drawnAnimation End, then Draw Begin, Draw, Draw End, Draw GUI Begin, Draw GUI, Draw GUI End
10the frame has been shown; now the instances destroyed in it are finished offDestroy, then Cleanup, for each instance destroyed this frame

Movement in step 5 is not an event: it applies speed and direction (or hspeed and vspeed), friction and gravity for you, after every Step has run. That is why code in Step sets a speed instead of moving x by hand, and why x still holds the frame's starting position during Step.

Two details in step 9. Animation End runs when a sprite's animation wraps past its last frame, and it runs before the Draw events. An object with no Draw event of its own has its sprite drawn automatically, after every object's Draw Begin and before any object's Draw. Once you write a Draw event for an object, that automatic drawing stops for it: call draw_self() in your Draw if you still want the sprite.

Who goes first

Within one step, the game handles one object at a time, in the order the objects are listed in your project. For each object, every instance of it runs the event, in slot order. That is usually the order they were made in, though a new instance can take the slot of one that was destroyed. Then the game moves on to the next object. So all of one object's Step events run before any of the next object's.

Setting a variable on another object. All of the ways work from any event: through an instance variable (other_car.hit = 1;), inside with (obj_car) { hit = 1; }, through other in a Collision event, and on the result of instance_nearest (or any call that gives an instance). The write is made the moment the line runs, and the other instance sees it from then on. What decides whether it reacts this frame is where that object stands in the order above. If the object that gets the write is listed after the one writing, its Step reads the new value in the same frame. If it is listed before, its Step for this frame has already run, so it reads the value on the next frame. The same goes for a Step that sets the variable back: if the receiving object's own Step clears or recounts the variable every frame (a hit timer it sets to 0, say), and it runs after the write, the write is gone before anything uses it. When a write seems to do nothing, look at which of the two objects runs first, and at what the receiving object's own Step does with that variable. To make the order certain, let the receiving object do the work: have it check in its own Step (with with (obj_rival), say) and apply the result to itself.

You never need to rely on that between objects. Your code can, though, when it matters: Collision states its own order as a promise, because it is the one event where two objects can each have code for the same meeting.

Events that are not part of the loop

Some events run at one moment in an instance's life or the game's, instead of on a frame schedule.

EventWhen it runs
Createonce, when the instance is made, right after its object's own settings (sprite, Visible, Solid...) are put on it
Destroyonce, at the end of the frame in which instance_destroy() was called on the instance (step 10) - not when it is called: the instance finishes that frame first
Cleanuponce, when the instance is really removed: at the end of the frame it was destroyed in, or when its room ends (a Persistent room is kept instead, and its instances get Cleanup only if the game later forgets the room)
Game Startonce per instance placed in the game's first room, only ever for that first room
Room Startonce per instance, when a room becomes the current one
Room Endonce per instance, as a room stops being the current one
Region Exited, Region Enteredonce per instance (Game Boy Advance, worlds only), when the player arrives in a different region than the last one: after the new part of the world has been made and after its Room Start, Exited first
User 0 to 15only when your own code, or a cutscene, asks for one by name with event_call

Destroy and Cleanup differ on purpose. Destroy is for something that has to happen because an instance was destroyed, such as spawning an explosion. It waits for the end of the frame, so the instance finishes the frame it was destroyed in first. Cleanup is for something that must happen whenever the instance is gone, however it went. A room change removes every instance that is not persistent and runs only Cleanup for it, never Destroy: leaving a room is not the instance being destroyed.

When a room opens

A room opens when the game starts, and again every time the game changes room (room_change, room_next, room_restart, or a door). It all happens once, in one go, before the room's first frame. This is the order:

How a room opens, step by stepHow a room opens, step by step
How a room opens, step by step
  1. Each placed instance is made, one at a time, in the order the room lists them. One instance finishes all of its own steps before the next one is made:

1. It is made where you put it in the room, so x and y already hold that place. 2. Its object's own settings go on it: the sprite, mask, Visible, Solid and Persistent boxes of the object editor. 3. Its object's Create event runs. 4. If it is a door, the settings the door dialog saved for it land now: where it leads, its number, whether it is locked, and its transition. 5. Its own creation code runs. That is the code you wrote for this one copy: right-click the copy in the Room Editor and choose Instance Creation Code. 6. If you gave it a path in the Room Editor, it starts following the path.

  1. Game Start runs for every instance, but only when this is the first room the game opens.
  2. The room's own creation code runs.
  3. Room Start runs for every instance now in the room, including any persistent ones that came along from the last room.

Only then does the room's first frame begin, at step 1 of the frame.

Coming back to a Persistent room the game kept is shorter. Steps 1 to 3 do not happen: nothing is made, so no Create, no creation code and no room creation code runs. The instances that were set aside are put back exactly as they were, and step 4 runs for them: Room Start runs for every instance in the room, the ones that came back included.

Creation code runs after Create, so anything that depends on a per-instance value goes there too. A per-instance value is a number, or some text, that only one placed copy has. Say every turret starts with ten shots, but one turret in the room should start with three:

// obj_turret, Create event: every turret starts the same way.
shots = 10;
// Creation code of ONE turret in the room. It runs after the Create event above.
shots = 3;

If Create had worked something out from shots (a bar 4 pixels wide for each shot, say), that sum would have used the 10 and would never hear about the 3. Work such a thing out where the number is final: in that creation code, or later, in Step or Draw.

One more thing follows from "one at a time": when an instance's Create runs, the instances after it in the room's list have not been made yet, so its Create cannot find them.

Frames where the steps are frozen

Steps 1 to 8 run only when the game is not paused (game_pause), no room transition is playing, and no cutscene is running. During any of those, no Step, Alarm, Button, Collision or room-edge event runs, and instances do not move.

Step 9 does not stop. The Draw events still run on those frames. That is what lets a pause menu, drawn in Draw GUI, appear on a frozen game, and why the screen keeps redrawing during a room transition. Animation End and the sprite animation behind it do run during a room transition and a cutscene, but not while the game is paused, unless the project unticked Stop animations on pause in Project Settings (it is ticked in every project).

Rules that catch people

Each event above has its own page with its exact rule and an example. These are the ones that surprise people most often:

See also: EventsThe order of a framestepcollisioncreategame_startroom_startregion_entergame_pause